package com.google.common.collect;

import com.getpebble.android.R;
import com.google.common.base.Preconditions;
import com.sun.mail.smtp.SMTPMessage;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;
import javax.annotation.Nullable;

public abstract class ImmutableSet<E> extends ImmutableCollection<E> implements Set<E> {
    private static final int CUTOFF = ((int) Math.floor(7.516192768E8d));

    static abstract class ArrayImmutableSet<E> extends ImmutableSet<E> {
        final transient Object[] elements;

        ArrayImmutableSet(Object[] elements) {
            this.elements = elements;
        }

        public int size() {
            return this.elements.length;
        }

        public boolean isEmpty() {
            return false;
        }

        public UnmodifiableIterator<E> iterator() {
            return asList().iterator();
        }

        public Object[] toArray() {
            return asList().toArray();
        }

        public <T> T[] toArray(T[] array) {
            return asList().toArray(array);
        }

        public boolean containsAll(Collection<?> targets) {
            if (targets == this) {
                return true;
            }
            if (!(targets instanceof ArrayImmutableSet)) {
                return super.containsAll(targets);
            }
            if (targets.size() > size()) {
                return false;
            }
            for (Object target : ((ArrayImmutableSet) targets).elements) {
                if (!contains(target)) {
                    return false;
                }
            }
            return true;
        }

        boolean isPartialView() {
            return false;
        }

        ImmutableList<E> createAsList() {
            return new RegularImmutableAsList((ImmutableCollection) this, this.elements);
        }
    }

    public abstract UnmodifiableIterator<E> iterator();

    public static <E> ImmutableSet<E> of() {
        return EmptyImmutableSet.INSTANCE;
    }

    public static <E> ImmutableSet<E> of(E element) {
        return new SingletonImmutableSet(element);
    }

    public static <E> ImmutableSet<E> of(E e1, E e2) {
        return construct(2, e1, e2);
    }

    public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... others) {
        Object[] elements = new Object[(others.length + 6)];
        elements[0] = e1;
        elements[1] = e2;
        elements[2] = e3;
        elements[3] = e4;
        elements[4] = e5;
        elements[5] = e6;
        System.arraycopy(others, 0, elements, 6, others.length);
        return construct(elements.length, elements);
    }

    private static <E> ImmutableSet<E> construct(int n, Object... elements) {
        switch (n) {
            case R.styleable.WalletFragmentStyle_buyButtonHeight /*0*/:
                return of();
            case SMTPMessage.RETURN_FULL /*1*/:
                return of(elements[0]);
            default:
                int tableSize = chooseTableSize(n);
                Object[] table = new Object[tableSize];
                int mask = tableSize - 1;
                int hashCode = 0;
                int i = 0;
                int uniques = 0;
                while (i < n) {
                    Object element = ObjectArrays.checkElementNotNull(elements[i], i);
                    int hash = element.hashCode();
                    int j = Hashing.smear(hash);
                    while (true) {
                        int uniques2;
                        int index = j & mask;
                        Object value = table[index];
                        if (value == null) {
                            uniques2 = uniques + 1;
                            elements[uniques] = element;
                            table[index] = element;
                            hashCode += hash;
                        } else if (value.equals(element)) {
                            uniques2 = uniques;
                        } else {
                            j++;
                        }
                        i++;
                        uniques = uniques2;
                    }
                }
                Arrays.fill(elements, uniques, n, null);
                if (uniques == 1) {
                    return new SingletonImmutableSet(elements[0], hashCode);
                }
                if (tableSize != chooseTableSize(uniques)) {
                    return construct(uniques, elements);
                }
                Object[] uniqueElements;
                if (uniques < elements.length) {
                    uniqueElements = ObjectArrays.arraysCopyOf(elements, uniques);
                } else {
                    uniqueElements = elements;
                }
                return new RegularImmutableSet(uniqueElements, hashCode, table, mask);
        }
    }

    static int chooseTableSize(int setSize) {
        int i = 1073741824;
        if (setSize < CUTOFF) {
            i = Integer.highestOneBit(setSize - 1) << 1;
            while (((double) i) * 0.7d < ((double) setSize)) {
                i <<= 1;
            }
        } else {
            Preconditions.checkArgument(setSize < 1073741824, "collection too large");
        }
        return i;
    }

    public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements) {
        if ((elements instanceof ImmutableSet) && !(elements instanceof ImmutableSortedSet)) {
            ImmutableSet<E> set = (ImmutableSet) elements;
            if (!set.isPartialView()) {
                return set;
            }
        } else if (elements instanceof EnumSet) {
            return ImmutableEnumSet.asImmutable(EnumSet.copyOf((EnumSet) elements));
        }
        return copyFromCollection(elements);
    }

    private static <E> ImmutableSet<E> copyFromCollection(Collection<? extends E> collection) {
        Object[] elements = collection.toArray();
        switch (elements.length) {
            case R.styleable.WalletFragmentStyle_buyButtonHeight /*0*/:
                return of();
            case SMTPMessage.RETURN_FULL /*1*/:
                return of(elements[0]);
            default:
                return construct(elements.length, elements);
        }
    }

    ImmutableSet() {
    }

    boolean isHashCodeFast() {
        return false;
    }

    public boolean equals(@Nullable Object object) {
        if (object == this) {
            return true;
        }
        if ((object instanceof ImmutableSet) && isHashCodeFast() && ((ImmutableSet) object).isHashCodeFast() && hashCode() != object.hashCode()) {
            return false;
        }
        return Sets.equalsImpl(this, object);
    }

    public int hashCode() {
        return Sets.hashCodeImpl(this);
    }
}
